package com.xj.qq.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.xj.qq.Chatting;
import com.xj.qq.R;
import com.xj.qq.adapters.ConversationAdapter;
import com.xj.qq.domain.Conversation;
import com.xj.qq.swipe.SwipeListView;

import java.util.ArrayList;

/**
 * Created by XJ on 2015/12/24
 */
public class ConversationFragment extends Fragment {

    private ArrayList<Conversation> cvs;
    public static int FLAG = -1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.conversation_fragment,container,false);
        SwipeListView ls = (SwipeListView) v.findViewById(R.id.ls_conversation);
        ConversationAdapter ca = new ConversationAdapter(getActivity(),cvs);
        ls.setAdapter(ca);
        ls.setOnTouchListener(new View.OnTouchListener() {
            float x;
            long time = 0;
            long downtime = 0;
            long uptime = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        FLAG = 0;
                        x = event.getX();
                        downtime = event.getDownTime();
                        break;
                    case MotionEvent.ACTION_UP:
                        FLAG = -1;
                        uptime = event.getEventTime();
                        time = uptime - downtime;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (event.getX() - x > 30){
                            FLAG = -1;
                            time = downtime = uptime = 0;
                        }
                        break;
                }
                if(time >= 500){
                    Intent intent = new Intent(getActivity(), Chatting.class);
                    startActivity(intent);
                }
                return false;
            }
        });
        return v;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        cvs = new ArrayList<>();

        for (int i =1; i < 15; i++){
            int random = (int) (Math.random() * 12);
            int iconId = R.drawable.img_0 + random;
            String name = "好友" + i + "号";
            String content = "再见！";
            String date = random + ":" + i%6 +"" + i%9;
            Conversation c = new Conversation(iconId,name,content,date);
            cvs.add(c);
        }

        super.onCreate(savedInstanceState);
    }
}
